﻿import inquirer from 'inquirer';
import * as shell from 'shelljs';
const { exec, echo, exit } = shell.default;

// 现代化颜色主题
const theme = {
    // 基础颜色
    reset: '\x1b[0m',
    bold: '\x1b[1m',
    dim: '\x1b[2m',

    // 前景色
    primary: '\x1b[38;5;75m', // 亮蓝色
    success: '\x1b[38;5;82m', // 亮绿色
    warning: '\x1b[38;5;220m', // 亮黄色
    error: '\x1b[38;5;196m', // 亮红色
    info: '\x1b[38;5;159m', // 青色
    purple: '\x1b[38;5;141m', // 紫色
    orange: '\x1b[38;5;208m', // 橙色
    gray: '\x1b[38;5;245m', // 灰色
    white: '\x1b[38;5;255m', // 白色

    // 背景色
    bgDark: '\x1b[48;5;235m', // 深灰背景
    bgBlue: '\x1b[48;5;24m', // 蓝色背景
    bgGreen: '\x1b[48;5;22m', // 绿色背景
    bgRed: '\x1b[48;5;52m', // 红色背景
};

// 现代化图标集
const icons = {
    rocket: '🚀',
    fire: '🔥',
    star: '⭐',
    gem: '💎',
    crown: '👑',
    magic: '✨',
    warning: '⚠️',
    success: '✅',
    error: '❌',
    info: 'ℹ️',
    folder: '📁',
    file: '📄',
    image: '🖼️',
    code: '💻',
    data: '📊',
    globe: '🌐',
    map: '🗺️',
    chat: '💬',
    bolt: '⚡',
    shield: '🛡️',
    key: '🔑',
    link: '🔗',
    clean: '🧹',
    trash: '🗑️',
    check: '✓',
    cross: '✗',
    arrow: '→',
    loading: '⏳',
};

// 格式化工具
const fmt = {
    title: text => `${theme.bold}${theme.primary}${text}${theme.reset}`,
    subtitle: text => `${theme.purple}${text}${theme.reset}`,
    success: text => `${theme.success}${text}${theme.reset}`,
    error: text => `${theme.error}${text}${theme.reset}`,
    warning: text => `${theme.warning}${text}${theme.reset}`,
    info: text => `${theme.info}${text}${theme.reset}`,
    highlight: text => `${theme.bold}${theme.white}${text}${theme.reset}`,
    dim: text => `${theme.dim}${theme.gray}${text}${theme.reset}`,
    orange: text => `${theme.orange}${text}${theme.reset}`,

    // 带背景的文本
    badge: (text, bg = theme.bgBlue) => `${bg}${theme.white}${theme.bold} ${text} ${theme.reset}`,

    // 渐变效果模拟
    gradient: text => {
        const colors = ['\x1b[38;5;75m', '\x1b[38;5;81m', '\x1b[38;5;87m', '\x1b[38;5;159m'];
        const chars = text.split('');
        return chars.map((char, i) => `${colors[i % colors.length]}${char}`).join('') + theme.reset;
    },
};

const questions = [
    {
        type: 'rawlist',
        message: '开发还是打包？',
        name: 'mode',
        choices: [
            { name: '本地开发', value: 'dev' },
            { name: '开始打包', value: 'build' },
        ],
        filter: function (val) {
            return val;
        },
    },
    {
        type: 'rawlist',
        message: '请选择打包环境:',
        when: function (answers) {
            return answers.mode === 'build';
        },
        name: 'name',
        choices: [
            { name: '开发环境', value: 'dev' },
            { name: '测试环境', value: 'test' },
            { name: 'uat环境', value: 'uat' },
            { name: 'sit环境', value: 'sit' },
            { name: '生产环境', value: 'prod' },
        ],
        filter: function (val) {
            return val;
        },
    },
];

inquirer.prompt(questions).then(function (answers) {
    if (answers.mode === 'dev') {
        if (exec(`pnpm dev`).code !== 0) {
            echo('Error: yarn build failed');
            exit(1);
        }
    } else {
        console.log();
        console.log(fmt.gradient('  ╔══════════════════════════════════════════════════════════════════╗'));
        console.log(fmt.gradient('  ║                                                                  ║'));
        console.log(
            `  ║               ${icons.rocket} ${fmt.title('MAMAK')} ${fmt.subtitle('· 开始打包中：')} ${fmt.success(
                answers.name
            )}${icons.magic}                      ║`
        );

        console.log(fmt.gradient('  ║                                                                  ║'));
        console.log(fmt.gradient('  ╚══════════════════════════════════════════════════════════════════╝'));
        console.log();

        if (exec(`pnpm build:${answers.name} --my=zyl`).code !== 0) {
            echo('Error: yarn build failed');
            exit(1);
        }
    }
});
